package com.campusflea.person;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.campusflea.DateBase.ShopInfo;
import com.campusflea.R;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;

/**
 * Created by Administrator on 2017/6/14.
 */

public class Person_Godown extends Activity implements SwipeRefreshLayout.OnRefreshListener{
    private RecyclerView mRecyclerview;
    private DemoAdapter mAdapter;
    private String phone;
    int colors[]={R.color.blue,
            R.color.red,
            R.color.darkorange

    };
    private static final int REFRESH_COMPLETE = 0X110;
    private SwipeRefreshLayout swipeRefreshLayout;
    private Handler mHandler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case REFRESH_COMPLETE:
//                    mDatas.addAll(Arrays.asList("Lucene", "Canvas", "Bitmap"));
                    mAdapter.notifyDataSetChanged();
                    swipeRefreshLayout.setRefreshing(false);
                    break;
            }
        };
    };
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_godown);
        Button btn_hui = (Button) findViewById(R.id.button7);
        btn_hui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent = getIntent();
        phone = intent.getExtras().getString("phone");
        swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
        swipeRefreshLayout.setOnRefreshListener(Person_Godown.this);
        mRecyclerview= (RecyclerView) findViewById(R.id.recyclerview_godown);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL,false));
        mAdapter=new DemoAdapter(this);
        mRecyclerview.setAdapter(mAdapter);
        initData();
    }

    private  void initData()
    {

        final String bql ="select * from ShopInfo where publisher = ?";
        final List<DataModel> mList=new ArrayList<>();
        final BmobQuery<ShopInfo> query=new BmobQuery<ShopInfo>();
        query.setSQL(bql);
        query.setPreparedParams(new Object[]{phone});
        query.doSQLQuery(new SQLQueryListener<ShopInfo>(){
            @Override
            public void done(BmobQueryResult<ShopInfo> bmobQueryResult, BmobException e) {
                List<ShopInfo>  list = (List<ShopInfo>) bmobQueryResult.getResults();
                if(list.size()==0){
                    Toast.makeText(Person_Godown.this,"获取信息失败",Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(Person_Godown.this,"信息加载中。。。。",Toast.LENGTH_LONG).show();
                    for(int i=0;i<list.size();i++){
                        int type=(int)((Math.random()*3)+1);
                        DataModel data=new DataModel();
                        data.avatarColor=colors[type-1];
                        data.url=list.get(i).getBmobFile().getUrl();
                        //getDynamicPicture().getUrl();
                        data.type=2;
                        data.name=list.get(i).getShop_name();
                        data.content=list.get(i).getShop_disc();
                        data.price=list.get(i).getShop_price();
                        data.contentColor=colors[(type+1)%3];
                        mList.add(data);
                    }
                    mAdapter.addList(mList);
                    mAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
    }
}
